package com.cloudkinto.cron;

import com.cloudkinto.service.job.AsyncJobService;
import com.cloudkinto.service.mail.MailService;
import com.cloudkinto.service.platform.sync.PlatformOrderSyncRecordService;
import com.cloudkinto.service.order.claim.ClaimService;
import com.cloudkinto.service.order.outbound.OutboundService;
import com.cloudkinto.service.platform.*;
import com.cloudkinto.service.platform.qoo10.PlatformOrderQoo10Service;
import com.cloudkinto.service.stock.ProductStockRackService;
import com.cloudkinto.service.stock.ProductStockService;
import com.cloudkinto.service.storage.StorageLocationService;
import com.cloudkinto.service.wr.QuotationSchemeService;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author zhangyy
 * @date 2020/6/18 16:03
 * @description: 开发环境的定时任务
 */
@ConditionalOnProperty(prefix = "gbc", name = "scheduling", havingValue = "dev")
@Component
@Logger
@RequiredArgsConstructor
public class CronDevService {
    private final ProductStockService productStockService;
    private final PlatformOrderAmazonSyncService platformOrderAmazonSyncService;
    private final PlatformOrderRakutenSyncService platformOrderRakutenSyncService;
    private final PlatformOrderShopifySyncService platformOrderShopifySyncOrderService;
    private final PlatformOrderMakeshopSyncService platformOrderMakeshopSyncService;
    private final PlatformOrderQoo10Service platformOrderQoo10Service;
    private final PlatformOrderYahooSyncService platformOrderYahooSyncService;
    private final MailService mailService;
    private final RedisTemplate redisTemplate;
    private final ClaimService claimService;
    private final PlatformOrderWowmaSyncService platformOrderWowmaSyncService;
    private final ProductStockRackService stockRackService;
    private final StorageLocationService storageLocationService;
    private final OutboundService outboundService;
    private final PlatformOrderService platformOrderService;
    private final PlatformOrderSyncRecordService platformOrderSyncRecordService;
    private final QuotationSchemeService quotationSchemeService;


    @Scheduled(fixedRate = 1000 * 60 * 60 * 80)
    public void yahooGetItemList() {
//        platformOrderYahooSyncService.getItemList();
    }
    @Scheduled(fixedRate = 1000 * 60 * 60 * 80)
    public void AmazonGetItemList() {
//        platformOrderAmazonSyncService.amazonGetItemList();
    }


//    @Scheduled(cron = "40 04,24,44 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 60 * 80)
    public void getAllGoodsInfo() {
//        platformOrderQoo10Service.getAllGoodsInfo();
    }

    /**
     * 自动同步qoo10取消订单
     */
//    @Scheduled(cron = "0 01,21,41 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void qoo10ClaimOrderSync() {
//        platformOrderQoo10Service.platOrderClaimQoo10Sync();
    }

    @Scheduled(cron = "00 */5 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void platformOrderAutoCheck() {
//        platformOrderService.autoCheck();
    }


    /**
     * 自动同步qoo10订单历史
     */
    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void qoo10OrderSyncHistory() {
//        platformOrderQoo10Service.platOrderQoo10SyncHistory();
    }

    /**
     * yahoo更新库存测试
     */
    @Scheduled(fixedRate = 1000 * 60 * 60 * 20)
    public void yahooSetStockTest() {
//        platformOrderRakutenSyncService.testSetStock();
//        platformOrderYahooSyncService.testSetStock();
//        platformOrderRakutenSyncService.itemSearch();
//        platformOrderRakutenSyncService.inventoryGet();
//        platformOrderRakutenSyncService.platOrderRakutenSync();
//        RakutenApiRes apiRes = platformOrderRakutenSyncService.testGetOrderByNumber("410368-20250319-0388530773", 16L);
//        System.out.println(apiRes);
    }

    /**
     * 获取jdy单号
     */
//    @Scheduled(cron = "40 04,24,44 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void getJdyTrackNumber() {
//        outboundService.getJdyTrackNumber();
    }



    /**
     * 推动订单到jdy
     */
//    @Scheduled(cron = "40 04,24,44 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void pushToJdCloud() {
//        outboundService.pushToJdCloud();
    }


    /**
     * 自动同步wowma订单
     */
//    @Scheduled(cron = "40 04,24,44 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void wowmaOrderSync() {
//        platformOrderWowmaSyncService.platOrderWowmaSync();
    }


    /**
     * 测试同步一个邮箱订单
     */
//    @Scheduled(cron = "20 03,13,23,33,43,53 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 10)
//    public void syncMailTest() {
//        MailDo mailDo = mailService.getById(27112);
//        mailService.syncMailOrder(new MailOrderConsumerReq(mailDo.getSubject(), mailDo.getTextPlain(), 5L, mailDo.getCompanyId()));
//    }


    /**
     * 自动同步yahoo订单
     */
//    @Scheduled(cron = "50 04,24,44 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void yahooOrderSync() {
//        platformOrderYahooSyncService.platOrderYahooSync();
    }

    /**
     * 定时同步邮箱
     */
    @Scheduled(cron = "20 03,13,23,33,43,53 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void syncMail() {
//        mailService.timingSynchronizationMail();
    }

    /**
     * 定时同步乐天标发状态
     */
//    @Scheduled(cron = "20 03,13,23,33,43,53 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void syncMarkRecordRakuten() {
//        platformOrderRakutenSyncService.syncMarkRecord();
    }

    /**
     * 定时同步亚马逊标发状态
     */
//    @Scheduled(cron = "10 01,11,21,31,41,51 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 1000)
    public void syncMarkRecordAmazon() {
//        platformOrderAmazonSyncService.syncStockToPlatformRecord();
//        platformOrderAmazonSyncService.syncMarkRecord();

    }

    /**
     * 自动同步亚马逊订单
     */
//    @Scheduled(cron = "0 18,38,58 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void amazonOrderSync() {
//        platformOrderAmazonSyncService.platOrderAmazonSync();
    }

    /**
     * 自动同步乐天订单
     */
//    @Scheduled(cron = "0 08,28,48 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void rakutenOrderSync() {
//        platformOrderRakutenSyncService.platOrderRakutenSync();
    }


    /**
     * 自动同步shopify订单
     */
    @Scheduled(cron = "0 04,24,44 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void shopifyOrderSync() {
//        platformOrderShopifySyncOrderService.platOrderShopifySync();
    }


    /**
     * 自动同步makeshop订单
     */
    @Scheduled(cron = "0 14,34,54 * * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void makeShopOrderSync() {
//        platformOrderMakeshopSyncService.platOrderMakeShopSync();
    }

    /**
     * 自动同步qoo10订单
     */
//    @Scheduled(cron = "0 11,31,51 * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void qoo10OrderSync() {
//        platformOrderQoo10Service.platOrderQoo10Sync();
    }



    /**
     * 仓储费每日计算
     */
//    @Scheduled(cron = "10 1 0 * * ?")
//    @Scheduled(fixedRate = 1000 * 60 * 60 * 8)
    public void stockFee() {
//        productStockService.getStockAgeList();
    }

    /**
     * 重置单号的序号
     */
    @Scheduled(cron = "0 0 0 1/7 * * ")
    public void resetOrderNoSeqNo() {
//        redisTemplate.opsForValue().set(RedisKeyConstants.ORDER_INPUT_NO_SEQ, 1);
//        redisTemplate.opsForValue().set(RedisKeyConstants.SHELF_NO_SEQ, 1);
//        redisTemplate.opsForValue().set(RedisKeyConstants.ORDER_OUTBOUND_NO_SEQ, 1);
//        redisTemplate.opsForValue().set(RedisKeyConstants.OUTBOUND_WAVE_NO_SEQ, 1);
//        redisTemplate.opsForValue().set(RedisKeyConstants.CANCEL_OUTBOUND_NO_SEQ, 1);
//        redisTemplate.opsForValue().set(RedisKeyConstants.CLAIM_NO_SEQ, 1);
    }

    /**
     * 每天执行
     */
    @Scheduled(cron = "0 0 4 * * ? ")
    public void executeEveryDay() {
        //认领单过期
//        claimService.lambdaUpdate()
//                .eq(ClaimDo::getStatus, ClaimStatusEnum.zero.getValue())
//                .lt(ClaimDo::getExpireTime, DateUtil.date())
//                .set(ClaimDo::getStatus, ClaimStatusEnum.three.getValue())
//                .update();
//        redisTemplate.opsForValue().set(RedisKeyConstants.WORK_ORDER_NO_SEQ, 1);
//        redisTemplate.opsForValue().set(RedisKeyConstants.PLAN_BATCH_NO_SEQ, 1);
//        Set<Long> rackIdList = stockRackService.lambdaQuery()
//                .gt(ProductStockRackDo::getQuantity, 0)
//                .or()
//                .gt(ProductStockRackDo::getLockQuantity, 0)
//                .list().stream().map(ProductStockRackDo::getRackId).collect(Collectors.toSet());
//        if (CollUtil.isNotEmpty(rackIdList)) {
//            storageLocationService.lambdaUpdate()
//                    .in(StorageLocationDo::getId, rackIdList)
//                    .set(StorageLocationDo::getEmptyLocationFlag, Boolean.FALSE)
//                    .update();
//            storageLocationService.lambdaUpdate()
//                    .notIn(StorageLocationDo::getId, rackIdList)
//                    .set(StorageLocationDo::getEmptyLocationFlag, Boolean.TRUE)
//                    .update();
//        } else {
//            storageLocationService.lambdaUpdate()
//                    .set(StorageLocationDo::getEmptyLocationFlag, Boolean.TRUE)
//                    .update();
//        }
    }
}